//
//  BWFTabBar.swift
//  网易新闻项目
//
//  Created by 薄文飞 on 16/3/2.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class BWFTabBar: UITabBar {

    var callBack : (() ->())?
    
    var index :Int = 0
    
    override init(frame: CGRect) {
    
        super.init(frame: frame)

        addSubview(composeButton)
        
        composeButton.addTarget(self, action: "composeButtonDidClick", forControlEvents: .TouchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        //必须先调用父类的方法
        super.layoutSubviews()
        
        let buttonWidth :CGFloat = self.bounds.size.width/5
        var index = 0
        
        for value in self.subviews {
        
            if  value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame = CGRectMake(CGFloat(index) * buttonWidth, 0, buttonWidth, self.bounds.size.height)
                
               index++
                
                if index == 2 {
                
                    index++
                }
                
            }
        }
        
        composeButton.center = CGPoint(x: self.center.x, y: self.bounds.size.height/2)
    }
    
    
    //懒加载中间的加号按钮
    private lazy var composeButton: UIButton = {
    
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        
        button.sizeToFit()
        
        
        return button
    }()
    
   @objc private func composeButtonDidClick() {
        
        //闭包回调
        callBack?()
        
    }

}
